package com.rh.handler;

import com.rh.dto.Message;
import com.rh.dto.Result;
import com.rh.excrptions.DoException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice

public class ExceptionHandlers {
    /**
     * 统一异常处理
     */
    @ResponseBody
    @ExceptionHandler(value = {DoException.class})
    public Result commonException(DoException e){
        e.printStackTrace();
        Result result = new Result();
        Message message=new Message();
        //设置状态码
        message.setCode(e.getCode());
        //设置响应消息
        message.setMsg(e.getMsg());
        result.setMessage(message);
       return  result;
    }

    @ExceptionHandler(value = {Exception.class})
    public String processException(Exception e){
        e.printStackTrace();
        if(e instanceof UnauthenticatedException){
            return "redirect:/toLogin";
        }else if(e instanceof UnauthorizedException){
            return "redirect:/unauthorized";
        }
        return "";
    }
}
